package com.diankangwo.service.impl;

import com.diankangwo.constant.MoneyCheckStatus;
import com.diankangwo.constant.PayStatus;
import com.diankangwo.dao.OnlineRefundLogDAO;
import com.diankangwo.dao.RefundCheckAccountDAO;
import com.diankangwo.dao.RefundMoneyDAO;
import com.diankangwo.domain.OnlineRefundLog;
import com.diankangwo.domain.RefundCheckAccount;
import com.diankangwo.domain.RefundMoney;
import com.diankangwo.dto.RefundCheckAccountDTO;
import com.diankangwo.service.RefundCheckAccountService;
import com.diankangwo.util.AmountUtils;
import com.diankangwo.util.PageSupport;
import com.diankangwo.util.QueryCAUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RefundCheckAccountServiceImpl implements RefundCheckAccountService {

	private static final Logger LOG = Logger.getLogger(RefundCheckAccountServiceImpl.class);

	@Autowired
	private RefundCheckAccountDAO refundCheckAccountDAO;

	@Autowired
	private RefundMoneyDAO refundMoneyDAO;

	@Autowired
	private OnlineRefundLogDAO onlineRefundLogDAO;

	@Override
	public void saveOrUpdate(RefundCheckAccount t) {
		try {
			refundCheckAccountDAO.saveOrUpdate(t);
		} catch (Exception e) {
			LOG.warn(e);
		}

	}

	@Override
	public void deleteById(Integer id) {
		try {
			refundCheckAccountDAO.deleteById(id);
		} catch (Exception e) {
			LOG.warn(e);
		}
	}

	@Override
	public PageSupport<RefundCheckAccount> findPagination(int pageNo, int pageSize) {
		try {
			return refundCheckAccountDAO.findPagination(pageNo, pageSize);
		} catch (Exception e) {
			LOG.warn(e);
			return null;
		}
	}

	@Override
	public boolean synchronousDate() {
		try {
			// 1.获取所有退费记录
			List<RefundMoney> rmList = refundMoneyDAO.findAll();
			if (null != rmList && rmList.size() > 0)
				for (RefundMoney rm : rmList) {
					if (rm.getState() == 3) {
						// 2.获取退费单号
						String refundNo = rm.getPaymenOrderNo();
						// 3.在退费日志中找到相关日志
						OnlineRefundLog orLog = onlineRefundLogDAO.findByProperty("refundNo", refundNo).get(0);
						// 4.比对支付状态并保存
						RefundCheckAccount rca = new RefundCheckAccount();
						rca.setRefundNo(refundNo);
						rca.setOrderNum(orLog.getList().size());
						rca.setBehooveMoney(rm.getMoney());
						rca.setActualMoney(orLog.getRefundFee());
						if (orLog.getWebhookStatus().trim().equals(PayStatus.success.getName())) {
							rca.setRemarks(MoneyCheckStatus.MCS_Normal.getValue());
						} else {
							rca.setRemarks(MoneyCheckStatus.MCS_Abnormal.getValue());
						}
						rca.setLastTime(new Date());
						rca.setCreateDate(new Date());
						rca.setCreateUser("user");
						rca.setUpdateDate(new Date());
						rca.setUpdateUser("user");
						refundCheckAccountDAO.saveOrUpdate(rca);
					} else {
						LOG.debug("未退款订单:" + rm.getPaymenOrderNo());
					}
				}
			return true;
		} catch (Exception e) {
			LOG.warn(e.getMessage());
			return false;
		}

	}

	@Override
	public Map<String, Object> count() {
		int sumBehooveMoney = 0, sumActualMoney = 0, sumRefundNo = 0, sumOrderNum = 0;
		Map<String,Object> sumMap = new HashMap<String, Object>();
		List<RefundCheckAccount> rcaList = refundCheckAccountDAO.findAll();
		for (RefundCheckAccount rca : rcaList) {
			sumBehooveMoney = sumBehooveMoney + rca.getBehooveMoney();
			sumActualMoney = sumActualMoney + rca.getActualMoney();
			sumRefundNo = sumRefundNo + 1;
			sumOrderNum = sumOrderNum + rca.getOrderNum();
		}
		sumMap.put("sumBehooveMoney", sumBehooveMoney);
		sumMap.put("sumActualMoney", sumActualMoney);
		sumMap.put("sumRefundNo", sumRefundNo);
		sumMap.put("sumOrderNum", sumOrderNum);
		return sumMap;
	}

	@Override
	public List<RefundCheckAccountDTO> queryCA(QueryCAUtil qcaUtil) throws Exception {
		String uniqueNo = qcaUtil.getUniqueNo();
		Integer pageNo = qcaUtil.getPageNo();
		Integer pageSize = qcaUtil.getPageSize();
		if(null != uniqueNo && !uniqueNo.equals("")){
			List<RefundCheckAccount> rcaList = refundCheckAccountDAO.findByProperty("refundNo", uniqueNo);
			return beanCopy(rcaList);
		}else{
			try {
				Date startTime = new SimpleDateFormat("yyyy-MM-dd").parse(qcaUtil.getStartTime());
				Date endTime = new SimpleDateFormat("yyyy-MM-dd").parse(qcaUtil.getEndTime());
				String hql = "from RefundCheckAccount rca where pca.lastTime between ? and ? order by rca.id desc";
				List<RefundCheckAccount> rcaList =refundCheckAccountDAO.findPagination(hql, pageNo, pageSize, startTime,endTime).getResultData();
				return beanCopy(rcaList);
			} catch (Exception e) {
				LOG.error(e.getMessage());
				List<RefundCheckAccount> rcaList = refundCheckAccountDAO.findList(pageNo, pageSize);
				return beanCopy(rcaList);
			}
			
		}
	}
	
	/**
	 * 转化金额显示和日期
	 * @param rcaList
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public static List<RefundCheckAccountDTO> beanCopy(List<RefundCheckAccount> rcaList) throws Exception{
		List<RefundCheckAccountDTO> rcadList = new ArrayList<RefundCheckAccountDTO>();
		for (RefundCheckAccount rca : rcaList) {
			RefundCheckAccountDTO rcad = new RefundCheckAccountDTO();
			BeanUtils.copyProperties(rcad, rca);
			rcadList.add(rcad);
		}
		for (RefundCheckAccountDTO rcad : rcadList) {
			rcad.setBehooveMoney(AmountUtils.changeF2Y(rcad.getBehooveMoney()));
			rcad.setActualMoney(AmountUtils.changeF2Y(rcad.getActualMoney()));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			 rcad.setLastTime((sdf.parse(rcad.getLastTime())).toLocaleString());
		}
		RefundCheckAccountDTO rcad = new RefundCheckAccountDTO();
		Collections.sort(rcadList,rcad);
		return rcadList;
		
	}

}
